package com.example.demo.utils;

import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

@Service
public class PoiUtils {


  @Autowired
  private  AppDataUtil appDataUtil;



  public  static Integer DocToPdf(String docPath,String pdfPath){
    Integer i =0;
    try {
      InputStream doc = new FileInputStream(docPath);
      XWPFDocument document = new XWPFDocument(doc);
      PdfOptions options = PdfOptions.create();
      OutputStream out = new FileOutputStream(pdfPath);
      PdfConverter.getInstance().convert(document, out, options);
      doc.close();
      out.close();
    }catch(Exception e) {
      i=15;
//      e.getMessage();
//      e.printStackTrace();
      System.out.println("转换失败"+e.getMessage());
    }finally {

    }
    return i;
  }



  public static Integer doc2pdf(String inPath, String outPath) {
    Integer i =0;
    try {
      long old = System.currentTimeMillis();
      File file = new File(outPath); // 新建一个空白pdf文档
      FileOutputStream os = new FileOutputStream(file);
      Document doc = new Document(inPath); // Address是将要被转化的word文档
      doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML,
      // OpenDocument, PDF,
      // EPUB, XPS, SWF 相互转换
      long now = System.currentTimeMillis();
      System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒"); // 转化用时
    } catch (Exception e) {
      i=15;
//      e.printStackTrace();
      System.out.println("转换失败"+e.getMessage());
    }
    return i;
  }


}
